package com.ruoyi.web.controller.nongjt.supervisors;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.nongjt.domain.pojo.NjtOrganization;
import com.ruoyi.nongjt.domain.pojo.NjtSupervisor;
import com.ruoyi.nongjt.service.NjtOrganizationService;
import com.ruoyi.nongjt.service.NjtSupervisorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nongjt/supervisor")
public class NjtSupervisorController extends BaseController {

    @Autowired
    private NjtSupervisorService supervisorService;

    @Autowired
    private NjtOrganizationService organizationService;

    /**
     * 查询监管者列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisor:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjtSupervisor supervisor) {
        startPage();
        List<NjtSupervisor> list = supervisorService.selectSupervisorList(supervisor);
        return getDataTable(list);
    }

    /**
     * 查询单位列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisor:list')")
    @GetMapping("/organizations")
    public AjaxResult organizations() {
        List<NjtOrganization> list = organizationService.selectOrganizationList(new NjtOrganization());
        return AjaxResult.success(list);
    }

    /**
     * 新增监管者
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisor:add')")
    @Log(title = "监管者管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NjtSupervisor supervisor) {
        if (supervisor.getOrganizationId() == null || supervisor.getOrganizationId() == 0) {
            return AjaxResult.error("请选择所属单位");
        }
        if (supervisor.getMobilePhone() == null || supervisor.getMobilePhone().isEmpty()) {
            return AjaxResult.error("请输入手机号码");
        }
        if (supervisor.getPassword() == null || supervisor.getPassword().isEmpty()) {
            return AjaxResult.error("请输入登录密码");
        }
        return toAjax(supervisorService.insertSupervisor(supervisor));
    }

    /**
     * 修改监管者
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisor:edit')")
    @Log(title = "监管者管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NjtSupervisor supervisor) {
        if (supervisor.getId() == null || supervisor.getId() == 0) {
            return AjaxResult.error("监管者ID不能为空");
        }
        return toAjax(supervisorService.updateSupervisor(supervisor));
    }

    /**
     * 删除监管者
     */
    @PreAuthorize("@ss.hasPermi('nongjt:supervisor:remove')")
    @Log(title = "监管者管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            supervisorService.deleteSupervisorById(id);
        }
        return success();
    }
}
